package com.demo.highSpeedMap.service;

import com.demo.highSpeedMap.entity.DoorDrivingInfo;
import com.demo.highSpeedMap.entity.MinStep;
import com.demo.highSpeedMap.entity.StandardDoorSerial;
import com.demo.highSpeedMap.mapper.DoorDrivingInfoMapper;
import com.demo.highSpeedMap.mapper.StandardDoorSerialMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;

@Service
public class StandardDoorSerialService {
    @Autowired
    StandardDoorSerialMapper standardDoorSerialMapper;
    @Autowired
    DoorDrivingInfoMapper doorDrivingInfoMapper;
    @Autowired
    DistanceDijkstraImpl distanceDijkstraImpl;

    public List<StandardDoorSerial> queryStandardDoorSerial(){
        return standardDoorSerialMapper.queryStandardDoorSerial();
    }


    public String[] getStrStep(String  start, String end){

        MinStep mainStep = getMinStep(start,end);
        List<String> stepList = mainStep.getStep();
        String[] strings =  new String[stepList.size()];

        for (int i= 0;i<stepList.size();i++) {
            List<DoorDrivingInfo> listDoor = doorDrivingInfoMapper.queryByDoorId(stepList.get(i));
            if (!listDoor.isEmpty()){
                DoorDrivingInfo drivingInfo = listDoor.get(0);
                StringBuilder strB = new StringBuilder();
                strB.append(drivingInfo.getDoorId()).append(",").append(drivingInfo.getLongitude()).append(",").append(drivingInfo.getLatitude());
                strings[i] = strB.toString();
            }

        }


       return strings;

    }





    public MinStep getMinStep(String  start, String end){

        HashMap<String, HashMap<String,Integer>> stepLength = getStepLength();
        return distanceDijkstraImpl.getMinStep(start,end,stepLength);
    }

    private  HashMap<String, HashMap<String,Integer>> getStepLength(){
        List<DoorDrivingInfo> listDoor  = doorDrivingInfoMapper.queryDoorDrivingInfo();
        List<StandardDoorSerial> list = standardDoorSerialMapper.queryStandardDoorSerial();
        HashMap<String, HashMap<String,Integer>> stepLength = new HashMap<String, HashMap<String,Integer>>();

        for (DoorDrivingInfo doorInfo:listDoor) {
            String doorId = doorInfo.getDoorId();
            HashMap<String,Integer> step = new HashMap<String, Integer>();
            List<StandardDoorSerial> listCurrent =  standardDoorSerialMapper.queryByCurrent(doorId);
            List<StandardDoorSerial> listNext =  standardDoorSerialMapper.queryByNext(doorId);

            for (StandardDoorSerial serial: listCurrent) {
                int apart = (int)serial.getApartFrom();
                step.put(serial.getNextDoor(),Integer.valueOf(apart));
            }

            for (StandardDoorSerial serial: listNext) {
                int apart = (int)serial.getApartFrom();
                step.put(serial.getCurrentDoor(),Integer.valueOf(apart));
            }

            stepLength.put(doorId, step);



        }

            return stepLength;

    }





}
